import requests  
import base64  
  
class APIClient:  
    def __init__(self, base_url, token):  
        self.base_url = base_url  
        self.token = token  
        self.headers = {  
            'Authorization': f'Bearer {token}',  
            'Content-Type': 'application/json'  
        }  
  
    def download_url(self, url):  
        endpoint = f"{self.base_url}/download"  
        try:  
            response = requests.get(endpoint, params={'url': url}, headers=self.headers)  
            response.raise_for_status()  # 如果响应状态码不是200，就主动抛出异常  
            data = response.json()  # 解析JSON响应内容  
            if data['code'] == 0:  # 假设APICode.Success是一个整数值  
                content = base64.b64decode(data['data']['content'])  
                return content.decode('utf-8')  # 返回解码后的内容  
            else:  
                raise ValueError(f"Error downloading URL: {data['msg']}")  
        except requests.RequestException as e:  
            raise ValueError(f"Request failed: {e}")  
        except ValueError as e:  
            raise ValueError(f"Response error: {e}")  
  
# 使用示例  
if __name__ == "__main__":  
    token = "your_token_here"  # 替换为你的token  
    base_url = "http://localhost:8080/v1"  # 替换为你的API基础URL  
    client = APIClient(base_url, token)  
    url_to_download = "https://example.com/somefile.txt"  # 替换为你想要下载的URL  
    try:  
        downloaded_content = client.download_url(url_to_download)  
        print(downloaded_content)  
    except ValueError as e:  
        print(f"An error occurred: {e}")